#include "StdAfx.h"
#include "option.h"
#include "tinyxml.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
option::option(void)
{	
    char* appPath = (char*)(void*)Marshal::StringToHGlobalAnsi(Environment::CurrentDirectory);
	char temp[1024] = "";
	configPath = (char*)malloc(sizeof(char)*1024);
	sprintf(configPath,"%s\\config.xml",appPath);
	//strcpy(this->configPath,temp);
	highlightedColor = Color::DarkSeaGreen;
	rdColor = Color::Red;
	this->readOptions();
	
	

}
bool option::saveOptions() {
		TiXmlDocument doc;
		TiXmlElement* element = new TiXmlElement("Config");
		element->SetAttribute("version","0.6");
		element->SetAttribute("bgColor",bgColor.ToArgb());
		element->SetAttribute("fontColor",fontColor.ToArgb());
		element->SetAttribute("linkColor",linkColor.ToArgb());
		element->SetAttribute("rootshColor",rootshColor.ToArgb());
		element->SetAttribute("ulColor",ulColor.ToArgb());
		element->SetAttribute("rootbgColor",rootbgColor.ToArgb());
		element->SetAttribute("rootRectColor",rootRectColor.ToArgb());
		//element->SetAttribute("serverURL",serverURL);
		element->SetAttribute("overrideColor",this->overrideColor);
		element->SetAttribute("saveLinks",saveLinks);
		element->SetAttribute("showHelp",showHelp);
		element->SetAttribute("showStartup",showStartup);
		element->SetAttribute("restorePrevious",restorePrevious);
		element->SetAttribute("showURLBar",showURLBar);
		element->SetAttribute("tabSelectorPos",tabSelectorPos);
		
		
		doc.LinkEndChild(element);
		
		return doc.SaveFile(this->configPath);
		 
};
bool option::readOptions() {
	TiXmlDocument doc;
	bool LoadOkay=doc.LoadFile(configPath);
	if(LoadOkay==false){
		this->restoreDefaults();
		return false;
	}
	TiXmlElement *element = doc.RootElement();
	bgColor = Color::FromArgb(atoi(element->Attribute("bgColor")));
	linkColor = Color::FromArgb(atoi(element->Attribute("linkColor")));
	fontColor = Color::FromArgb(atoi(element->Attribute("fontColor")));
	rootRectColor = Color::FromArgb(atoi(element->Attribute("rootRectColor"))); //tutaj zmiana potrzebna
	rootbgColor = Color::FromArgb(atoi(element->Attribute("rootbgColor")));
	rootshColor = Color::FromArgb(atoi(element->Attribute("rootshColor")));
	ulColor = Color::FromArgb(atoi(element->Attribute("ulColor")));
	overrideColor = atoi(element->Attribute("overrideColor"))!=0;
	saveLinks = atoi(element->Attribute("saveLinks"))!=0;
	showHelp = atoi(element->Attribute("showHelp"))!=0;
	showStartup = atoi(element->Attribute("showStartup"))!=0;
	restorePrevious = atoi(element->Attribute("restorePrevious"))!=0;
	showURLBar = atoi(element->Attribute("showURLBar"))!=0;
	tabSelectorPos = atoi(element->Attribute("tabSelectorPos"));
	//serverURL = element->Attribute("serverURL");
	return true;



}

void option::restoreDefaults() {
	
	fontColor = Color::Black;
	ulColor = Color::Black;
    bgColor = Color::White;
	rdColor = Color::Red;
	rootbgColor = Color::White;
	rootRectColor= Color::Black;
	rootshColor = Color::LightGray;
	linkColor = Color::Black; 
	overrideColor = false;
	highlightedColor = Color::DarkSeaGreen;
	saveLinks = true;
	showHelp = true;
	showStartup = false;
	restorePrevious = false;
	showURLBar= false;
	tabSelectorPos = 0;
	serverURL = "http://maephisto.no-ip.info/";
}